﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Coleotrack.Data
{
    public abstract class ValueObject<T> : IEquatable<T> where T: ValueObject<T>
    {
        public virtual bool Equals(T other)
        {
            if (other == null)
            {
                return false;
            }

            Type leftType = GetType();
            Type otherType = other.GetType();
            if (leftType != otherType)
            {
                return false;
            }

            IEnumerable<FieldInfo> fields = GetFieldsFromTypeHierarchy();
            foreach (FieldInfo field in fields)
            {
                object value1 = field.GetValue(this);
                object value2 = field.GetValue(other);

                if (value1 == null)
                {
                    if (value2 != null)
                    {
                        return false;
                    }
                }
                else if (!value1.Equals(value2))
                {
                    return false;
                }
            }

            return true;
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            return Equals(obj as T);
        }

        public override int GetHashCode()
        {
            IEnumerable<FieldInfo> fields = GetFieldsFromTypeHierarchy();

            const int startValue = 17;
            const int multiplier = 59;

            int hashCode = startValue;
            foreach (FieldInfo field in fields)
            {
                object value = field.GetValue(this);

                hashCode = (hashCode * multiplier) + (value != null ? value.GetHashCode() : startValue);
            }

            return hashCode;
        }

        IEnumerable<FieldInfo> GetFieldsFromTypeHierarchy()
        {
            Type t = GetType();

            var fields = new List<FieldInfo>();
            while (t != typeof(object))
            {
                fields.AddRange(t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));

                t = t.BaseType;
            }

            return fields;
        }

        public static bool operator ==(ValueObject<T> left, ValueObject<T> right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(ValueObject<T> left, ValueObject<T> right)
        {
            return !Equals(left, right);
        }
    }
}
